package com.kystar.muxplayer.ui;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;

/**
 * Created by zhusheng on 2018-03-11.
 */

public class OkCancelView {
    View rootView;
    ViewList viewList;
    Runnable okRunnable;
    Runnable cancelRunnable;

    public OkCancelView(View rootView) {
        this.rootView = rootView;
        this.viewList = new ViewList();
        rootView.findViewById(R.id.button_ok).setOnClickListener(v -> this.ok());
        rootView.findViewById(R.id.button_cancel).setOnClickListener(v-> this.cancel());
    }

    public void ok() {
        if(okRunnable != null) {
            okRunnable.run();
        }
        hide();
    }

    public void cancel() {
        if(cancelRunnable != null) {
            cancelRunnable.run();
        }
        hide();
    }

    public OkCancelView onOk(Runnable run) {
        this.okRunnable = run;
        return this;
    }

    public OkCancelView onCancel(Runnable run) {
        this.cancelRunnable = run;
        return this;
    }

    public void show() {
        rootView.setVisibility(View.VISIBLE);
    }

    public void hide() {
        Helper.each(viewList, view -> view.setSelected(false));
        rootView.setVisibility(View.INVISIBLE);
    }

    public OkCancelView watch(TextView...views) {
        return watch(false, views);
    }

    public OkCancelView watch(boolean hasLabel, TextView...views) {
        viewList.clear();
        for (TextView v: views) {
            TextView label = null;
            if (hasLabel) {
                label = getLabel(v);
                viewList.add(label);
            }
            viewList.add(v);
            v.addTextChangedListener(new Watcher(v, label));
        }
        return this;
    }

    public OkCancelView watch(RectGroupView group) {
        watch(true, group.left, group.top, group.width, group.height);
        return this;
    }

    public void setSelected(boolean selected) {
        viewList.each(view -> view.setSelected(selected));
    }

    public static TextView getLabel(TextView view) {
        return (TextView) ((ViewGroup)view.getParent()).getChildAt(0);
    }

    public class Watcher implements TextWatcher {
        private TextView label;
        private TextView view;

        public Watcher(TextView view, TextView label) {
            this.label = label;
            this.view = view;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (label != null) {
                label.setSelected(true);
            }
            if (view != null) {
                view.setSelected(true);
            }
            show();
        }

    }
}
